using System;
using System.Windows.Forms;
using WegasManagementSystem.Models;

namespace WegasManagementSystem.Forms
{
    /// <summary>
    /// 考勤信息编辑表单
    /// </summary>
    public partial class AttendanceEditForm : Form
    {
        /// <summary>
        /// 考勤信息对象
        /// </summary>
        public AttendanceInfo AttendanceInfo { get; set; }

        /// <summary>
        /// 是否为添加操作
        /// </summary>
        public bool IsAdd { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public AttendanceEditForm()
        {
            InitializeComponent();
        }

        private void AttendanceEditForm_Load(object sender, EventArgs e)
        {
            if (AttendanceInfo != null)
            {
                // 显示员工和部门信息
                txtEmpName.Text = AttendanceInfo.EmpName;
                txtDeptName.Text = AttendanceInfo.DeptName;
                txtYear.Text = AttendanceInfo.Year.ToString();
                txtMonth.Text = AttendanceInfo.Month.ToString();

                // 设置各数值
                numWorkDays.Value = AttendanceInfo.WorkDays;
                numActualWorkDays.Value = AttendanceInfo.ActualWorkDays;
                numOvertimeHours.Value = AttendanceInfo.OvertimeHours;
                numLateCount.Value = AttendanceInfo.LateCount;
                numEarlyLeaveCount.Value = AttendanceInfo.EarlyLeaveCount;
                txtAbsenceDays.Text = AttendanceInfo.AbsenceDays.ToString();

                // 设置窗体标题
                this.Text = IsAdd ? "添加考勤记录" : "编辑考勤记录";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (AttendanceInfo != null)
            {
                // 更新考勤信息对象的值
                AttendanceInfo.WorkDays = numWorkDays.Value;
                AttendanceInfo.ActualWorkDays = numActualWorkDays.Value;
                AttendanceInfo.AbsenceDays = AttendanceInfo.WorkDays - AttendanceInfo.ActualWorkDays;
                if (AttendanceInfo.AbsenceDays < 0)
                {
                    AttendanceInfo.AbsenceDays = 0;
                }
                AttendanceInfo.OvertimeHours = numOvertimeHours.Value;
                AttendanceInfo.LateCount = (int)numLateCount.Value;
                AttendanceInfo.EarlyLeaveCount = (int)numEarlyLeaveCount.Value;

                // 设置对话框结果为OK
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void numActualWorkDays_ValueChanged(object sender, EventArgs e)
        {
            // 实际出勤天数不能超过应出勤天数
            if (numActualWorkDays.Value > numWorkDays.Value)
            {
                numActualWorkDays.Value = numWorkDays.Value;
            }

            // 计算并显示缺勤天数
            decimal absenceDays = numWorkDays.Value - numActualWorkDays.Value;
            if (absenceDays < 0)
            {
                absenceDays = 0;
            }
            txtAbsenceDays.Text = absenceDays.ToString("0.##");
        }

        private void numWorkDays_ValueChanged(object sender, EventArgs e)
        {
            // 应出勤天数变化时，确保实际出勤天数不超过应出勤天数
            if (numActualWorkDays.Value > numWorkDays.Value)
            {
                numActualWorkDays.Value = numWorkDays.Value;
            }
            else
            {
                // 重新计算缺勤天数
                decimal absenceDays = numWorkDays.Value - numActualWorkDays.Value;
                if (absenceDays < 0)
                {
                    absenceDays = 0;
                }
                txtAbsenceDays.Text = absenceDays.ToString("0.##");
            }
        }

        // 将btnOK的点击事件处理程序重命名为btnSave
        private void btnOK_Click(object sender, EventArgs e)
        {
            btnSave_Click(sender, e);
        }
    }
}
